package org.wxd.bi.test;

import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wxd.bi.api.PushApiHttp;
import org.wxd.bi.api.po.pushlog.LoginRoleLog;
import org.wxd.bi.api.po.pushlog.LoginUserLog;
import org.wxd.bi.api.struct.PushColumn;
import org.wxd.bi.api.struct.PushMapping;
import org.wxd.format.IdFormat;
import org.wxd.net.web.template.HtmlStyle;
import org.wxd.timer.TimeUtil;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author: Troy.Chen(無心道, 15388152619)
 * @version: 2021-09-30 15:05
 **/
public class PushLog implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger log = LoggerFactory.getLogger(PushLog.class);

    PushApiHttp pushApiHttp1001 = new PushApiHttp(10001, "OY9GIn9Xz8wq05Toa39wu7osDNtcXAk4");

    @Test
    public void pushBagData() throws InterruptedException {
        {
            /*todo 注册表结构*/
            PushMapping pushMapping = new PushMapping()
                    .setTableName("baglog")
                    .setTableComment("背包日志")
                    .setShowIndex(3);

            final HtmlStyle htmlStyle = new HtmlStyle();
            htmlStyle.setValue("width: 200px");
            pushMapping.getColumnList().add(new PushColumn("playerId", long.class.getSimpleName(), "用户id", true).setHtmlStyle(htmlStyle));
            pushMapping.getColumnList().add(new PushColumn("playerName", String.class.getSimpleName(), "用户账号", true));
            pushMapping.getColumnList().add(new PushColumn("itemId", int.class.getSimpleName(), "道具id", true));
            pushMapping.getColumnList().add(new PushColumn("itemCount", int.class.getSimpleName(), "道具数量", true));

            pushApiHttp1001.pushMapping(pushMapping, 1);
        }
        {
            /*todo 注册数据*/
            for (int i = 0; i < 200; i++) {
                int nextInt = ThreadLocalRandom.current().nextInt(10000, 20000);
                JSONObject jsonObject = new JSONObject(true);
                jsonObject.put("playerId", nextInt);
                jsonObject.put("playerName", String.valueOf(nextInt));
                jsonObject.put("itemId", ThreadLocalRandom.current().nextInt(10200, 10300));
                jsonObject.put("itemCount", ThreadLocalRandom.current().nextInt(2, 10));

                pushApiHttp1001.pushData("baglog", jsonObject.toString());
            }
            pushApiHttp1001.getWork().close();
        }
    }

    @Test
    public void pushRechargeData() throws InterruptedException {
        {
            /*todo 注册表结构*/
            PushMapping pushMapping = new PushMapping()
                    .setTableName("rechargelog")
                    .setTableComment("充值日志");

            final HtmlStyle htmlStyle = new HtmlStyle();
            htmlStyle.setValue("width: 200px");
            pushMapping.getColumnList().add(new PushColumn("playerId", long.class.getSimpleName(), "用户id", true).setHtmlStyle(htmlStyle));
            pushMapping.getColumnList().add(new PushColumn("playerName", String.class.getSimpleName(), "用户账号", true));

            pushApiHttp1001.pushMapping(pushMapping, 4);
        }
        {
            /*todo 注册数据*/
            for (int i = 0; i < 200; i++) {
                int nextInt = ThreadLocalRandom.current().nextInt(10000, 20000);
                JSONObject jsonObject = new JSONObject(true);
                jsonObject.put("playerId", nextInt);
                jsonObject.put("playerName", String.valueOf(nextInt));

                pushApiHttp1001.pushData("rechargelog", jsonObject.toString());
            }
            pushApiHttp1001.getWork().close();
        }
    }

    /**
     * push 登录数据，模拟登录日志
     *
     * @throws InterruptedException
     */
    @Test
    public void pushLoginUser() throws InterruptedException {
        long startTimeOffDay = TimeUtil.startTimeOffDay(-40);
        IdFormat build = IdFormat.build();
        Map<Long, Long> userinfo = new LinkedHashMap();

        for (int i = 0; i < 40; i++) {
            System.out.println(TimeUtil.formatDateString(startTimeOffDay));
            startTimeOffDay = TimeUtil.startTimeOffDay(startTimeOffDay, 1);
            int nextInt = ThreadLocalRandom.current().nextInt(20, 40);
            for (int k = 0; k < nextInt; k++) {
                userinfo.put(build.getId(), startTimeOffDay);
            }
            for (Map.Entry<Long, Long> entry : userinfo.entrySet()) {

                int dayOffset = TimeUtil.dayOffset(entry.getValue(), startTimeOffDay) + 1;
                if (dayOffset > 1) {
                    int anInt = ThreadLocalRandom.current().nextInt(1, 10000);
                    if (anInt < 3500) {
                        continue;
                    }
                }
                LoginUserLog loginLog = new LoginUserLog()
                        .setUserId(entry.getKey())
                        .setUserName(String.valueOf(entry.getKey()))
                        .setCreateTime(entry.getValue())
                        .setChannel("xiaomi")
                        .setOs("and")
                        .setImei("imie")
                        .setIp("127.0.0.1")
                        .setRegion("本地")
                        .setExpand("本地")
                        .setSaveTime(startTimeOffDay);
                pushApiHttp1001.pushData(loginLog);
            }
        }
        pushApiHttp1001.getWork().close();
    }

    /**
     * push 登录数据，模拟登录日志
     *
     * @throws InterruptedException
     */
    @Test
    public void pushLoginRole() throws InterruptedException {
        long startTimeOffDay = TimeUtil.startTimeOffDay(-40);
        IdFormat build = IdFormat.build();
        Map<Long, Long> roleInfo = new LinkedHashMap();

        for (int i = 0; i < 40; i++) {
            System.out.println(TimeUtil.formatDateString(startTimeOffDay));
            startTimeOffDay = TimeUtil.startTimeOffDay(startTimeOffDay, 1);
            int nextInt = ThreadLocalRandom.current().nextInt(20, 40);
            for (int k = 0; k < nextInt; k++) {
                roleInfo.put(build.getId(), startTimeOffDay);
            }
            for (Map.Entry<Long, Long> entry : roleInfo.entrySet()) {

                int dayOffset = TimeUtil.dayOffset(entry.getValue(), startTimeOffDay) + 1;
                if (dayOffset > 1) {
                    int anInt = ThreadLocalRandom.current().nextInt(1, 10000);
                    if (anInt < 3500) {
                        continue;
                    }
                }
                LoginRoleLog loginLog = new LoginRoleLog()
                        .setSaveTime(startTimeOffDay)
                        .setRoleId(entry.getKey())
                        .setRoleName(String.valueOf(entry.getKey()))
                        .setCreateTime(entry.getValue())
                        .setUserId(entry.getKey())
                        .setUserName(String.valueOf(entry.getKey()))
                        .setServerId(ThreadLocalRandom.current().nextInt(1, 4))
                        .setServerName("test")
                        .setChannel("xiaomi")
                        .setIp("127.0.0.1")
                        .setRegion("本地")
                        .setExpand("本地");
                pushApiHttp1001.pushData(loginLog);
            }
        }
        pushApiHttp1001.getWork().close();
    }

}
